//
//  MXBottomView.m
//  MXArcChart
//
//  Created by 马彦 on 16/11/9.
//  Copyright © 2016年 MX. All rights reserved.
//

#import "MXBottomView.h"

@interface MXBottomView ()

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, assign) CGFloat bottomWidth;

@end

@implementation MXBottomView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.legendStyle = ChartLegendStyleSquare;
    }
    return self;
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
}


- (void)setColorArray:(NSArray *)colorArray {
    _colorArray = colorArray;
//    NSLog(@"setColorArray++++++++--------");
}

- (void)setNameArray:(NSArray *)nameArray {
    _nameArray = nameArray;
    for (int i = 0; i < _nameArray.count; i++) {
        NSDictionary * dict = @{NSFontAttributeName: [UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[UIColor blackColor]};
        CGSize size = [_nameArray[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
        CGFloat x = self.height * 0.5 +((30 + self.height * 0.5 + size.width) * (i % 3));
        self.bottomWidth = x + 20 + size.width;
    }
    if ([_delegate respondsToSelector:@selector(bottomViewWidth:)]) {
        [_delegate bottomViewWidth:(self.bottomWidth)];
    }
}

- (void)setNameColor:(UIColor *)nameColor {
    _nameColor = nameColor;
//    NSLog(@"setNameColor+++++++------");
}

- (void)setLegendStyle:(ChartLegendStyle)legendStyle {
    _legendStyle = legendStyle;
    
//    NSLog(@"-----++++++++++---");
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    NSLog(@"bottomView-layoutSubviews---");
}

- (void)drawRect:(CGRect)rect {
    if (!_nameArray.count || !_colorArray.count) {
        return;
    }
    NSLog(@"drawRect-----");
    self.backgroundColor = [UIColor grayColor];
    for (int i = 0; i < _colorArray.count; i++) {
        NSDictionary * dict = @{NSFontAttributeName: [UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[UIColor blackColor]};
        
        CGSize size = [_nameArray[i] boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
        CGFloat x = self.height * 0.5 +((30 + self.height * 0.5 + size.width) * (i % 3));
        CGFloat y = self.height * 0.5;
        switch (self.legendStyle) {
            case ChartLegendStyleCircle:
                    [self drawLineRoundWithcenter:CGPointMake( x, y) radius:self.height * 0.5 startAngle:0 endAngle:M_PI * 2.0 color:_colorArray[i] fill:YES];
              
                break;
            case ChartLegendStyleSquare:
                [self chartBaseSquareWithRect:CGRectMake( x - self.height * 0.5, 0, self.height, self.height) color:_colorArray[i] fill:YES];
                break;
            default:
                
                break;
        }
       
        
        [self drawText:_nameArray[i] point:CGPointMake(x + 15, y - size.height * 0.5) color:_colorArray[i] Attributes:dict];
    }
    
   
    
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        [self addSubview:_contentView];
    }
    return _contentView;
}


@end
